package cn.tom.tool;

import cn.tom.dao.BookMapper;
import cn.tom.dao.LargeFileDao;
import cn.tom.dao.TypeMapper;
import cn.tom.entity.Book;
import cn.tom.entity.LargeFile;
import cn.tom.entity.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Component
public class InitData {
    @Autowired
    RedisCache redisCache;
    @Autowired
    BookMapper bookMapper;
    @Autowired
    LargeFileDao largeFileDao;
    @Autowired
    TypeMapper typeMapper;

    public void books() {
        redisCache.deleteObject("books");
        Map<Integer, Book> bookStock = bookMapper.findAll();
        Map<String,Object> map = new HashMap<>();
        for (Map.Entry<Integer, Book> bookEntry : bookStock.entrySet()) {
            map.put(String.valueOf(bookEntry.getKey()),bookEntry.getValue());
        }
        redisCache.setCacheMap("books",map);
    }

    public void stock(){
        /*库存*/
        redisCache.deleteObject("stock");
        Map<Integer, Book> allStock = bookMapper.findAllStock();

        Map<String,Integer> stockMap = new HashMap<>();
        for (Map.Entry<Integer, Book> bookEntry : allStock.entrySet()) {
            stockMap.put(String.valueOf(bookEntry.getKey()),bookEntry.getValue().getStock());
        }
        redisCache.setCacheMap("stock",stockMap);
    }

    public void setLargeFile() {
        /*图片*/

        List<LargeFile> largeFileList = largeFileDao.findAll();
        for (LargeFile largeFile : largeFileList) {
            redisCache.setCacheObject(largeFile.getId(),largeFile,60*24, TimeUnit.MINUTES);
        }
    }

    public void findTypeAll2() {
        /*type*/
        redisCache.deleteObject("findTypeAll2");
        List<Type> findAll2 = typeMapper.findAll2();
        redisCache.setCacheList("findTypeAll2",findAll2);
    }
}
